package com.ruoyi.quartz.task;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {
    @Value("${spring.datasource.druid.master.username}")
    private String dbUsername;

    @Value("${spring.datasource.druid.master.password}")
    private String dbPassword;


    /**
     * @description 备份Mysql的目录
     * @author let_free
     * @date 2024/11/21 10:08
     * @param null
     * @return null
     */
    @Value("${mysqlDumpPath}")
    private String mysqlDumpPath;


    public void backupDatabase(String dbName) throws IOException, InterruptedException {
        // 创建备份文件路径的目录（如果不存在）
// 获取当前日期的时间戳
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String timestamp = dateFormat.format(Calendar.getInstance().getTime());

        // 创建备份文件路径的目录（如果不存在）
        String timestampPath = mysqlDumpPath + timestamp + "/";
        File timestampDir = new File(timestampPath);
        if (!timestampDir.exists()) {
            timestampDir.mkdirs();
        }
        // 构建备份文件名
        String fileName = timestampPath + dbName + ".sql";

        // 构建备份命令
        String command = String.format("mysqldump -u%s -p%s %s > %s", dbUsername, dbPassword, dbName, fileName);

        // 执行备份命令
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("/bin/sh", "-c", command);

        Process process = processBuilder.start();
        int exitCode = process.waitFor();

        if (exitCode != 0) {
            throw new RuntimeException("Backup failed with exit code " + exitCode);
        }
    }

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }
}
